package com.ycy.ss.sync;

import java.net.URLEncoder;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.ycy.ss.Constant;
import com.ycy.ss.SSUtil;
import com.ycy.ss.status.AllStatus;
import com.ycy.ss.tumblr.TumblrUtil;
import com.ycy.ss.tumblr.po.TumblrPostBase;
import com.ycy.ss.tumblr.po.TumblrPostLink;
import com.ycy.ss.tumblr.po.TumblrPostPhotos;
import com.ycy.ss.tumblr.po.TumblrPostText;
import com.ycy.util.httpUtils.HttpRequest;
import com.ycy.util.httpUtils.HttpResponse;
import com.ycy.util.httpUtils.HttpUtil;

import weibo4j.Timeline;
import weibo4j.http.ImageItem;
import weibo4j.model.Status;

public class WriteInfo{
	private static Logger logger = LogManager.getLogger(WriteInfo.class);

	public static void write(Info info, String siteName){
		if ("tumblr".equals(siteName)) {
			_writeTumblr(info);
		} else if ("sina".equals(siteName)) {
			_writeSina(info);
		}
	}

	private static void _writeSina(Info info){
		logger.info("write info to sina.");
		if (AllStatus.sina == null) {
			logger.warn("AllStatus.sina is null.");
			return;
		} else {
			Timeline tm = new Timeline();

			Status res = null;
			try {
				String type = info.getType();
				if (AllType.TEXT.equals(type)) {
					String status = info.getInfoUrl() + " " + info.getContent();
					status = SSUtil.cutString(status, 140);
					res = tm.UpdateStatus(status);
				} else if (AllType.LINK.equals(type)) {
					String status = info.getInfoUrl() + " from:"
							+ SSUtil.shortLinkbyUrl(info.getAttachmentUrl()) + " "
							+ info.getContent();
					status = SSUtil.cutString(status, 140);
					res = tm.UpdateStatus(status);
				} else if (AllType.PHOTO.equals(type)) {
					String status = info.getInfoUrl() + " " + info.getContent();
					status = SSUtil.cutString(status, 140);

					try {
						HttpRequest req = new HttpRequest();
						req.setUrl(info.getAttachmentUrl()).setNeedBody(true);
						HttpResponse resp = new HttpUtil().setPost(false).request(req);
						ImageItem item = new ImageItem(resp.getByteBody());
						res = tm.UploadStatus(URLEncoder.encode(status, "UTF-8"), item);
					} catch (Exception e) {
						logger.warn("", e);
					}
				} else {
					logger.warn("_writeSina: does not support this type:" + type);
				}

				if (res != null) {
					AllStatus.sina.ok();
				}

			} catch (Exception e) {
				logger.warn("", e);
				AllStatus.sina.error(e.getMessage());
			}
		}
		logger.info("write info to sina ok.");
	}

	private static void _writeTumblr(Info info){
		logger.info("write info to tumblr.");
		if (AllStatus.tumblr == null) {
			logger.warn("AllStatus.tumblr is null.");
			return;
		} else {
			TumblrPostBase post = null;

			try {
				String type = info.getType();
				if (AllType.TEXT.equals(type)) {
					post = new TumblrPostText();
					post.setType(AllType.TEXT);

					String body = info.getInfoUrl() + " " + info.getContent();
					((TumblrPostText) post).setBody(body);
				} else if (AllType.PHOTO.equals(type)) {
					post = new TumblrPostPhotos();
					post.setType(AllType.PHOTO);

					String caption = info.getInfoUrl() + " " + info.getContent();
					((TumblrPostPhotos) post).setCaption(caption);
					((TumblrPostPhotos) post).setSource_url(info.getAttachmentUrl());
				} else if (AllType.LINK.equals(type)) {
					post = new TumblrPostLink();
					post.setType(AllType.LINK);

					String title = SSUtil.cutString(info.getContent(), 40);
					String description = info.getInfoUrl() + " " + info.getContent();
					((TumblrPostLink) post).setTitle(title);
					((TumblrPostLink) post).setDescription(description);
					((TumblrPostLink) post).setUrl(info.getAttachmentUrl());
				} else {
					logger.warn("_writeTumblr: does not support this type:" + type);
				}

				if (post != null) {
					String date = TumblrUtil.newPost(AllStatus.tumblr.ACCESS_TOKEN, Constant
							.getAccountInfo("tumblr.baseHostname"), post);
					if (date != null) {
						AllStatus.tumblr.ok();
					}
				}

			} catch (Exception e) {
				logger.warn("", e);
				AllStatus.tumblr.error(e.getMessage());
			}

		}
		logger.info("write info to tumblr ok.");
	}

}
